// ignore_for_file: non_constant_identifier_names

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:verse_systems/commom/global/global.dart';
import 'package:verse_systems/commom/models/application_model.dart';
import 'package:verse_systems/commom/models/apply_model.dart';
import 'package:verse_systems/commom/models/loan_model.dart';
import 'package:verse_systems/pages/orderdetails/order_details_control.dart';
import 'package:verse_systems/res/colors.dart';
import 'package:verse_systems/utils/image_utils.dart';
import 'package:verse_systems/widgets/dashed_line.dart';
import 'package:verse_systems/widgets/drawer_appbar.dart';
import 'package:verse_systems/widgets/order_empty.dart';
import 'package:verse_systems/widgets/sp_button.dart';

class OrderDetailsPage extends StatefulWidget {
  const OrderDetailsPage({super.key});

  @override
  State<OrderDetailsPage> createState() => _OrderDetailsPageState();
}

class _OrderDetailsPageState extends State<OrderDetailsPage> {
  final control = Get.find<OderDetailsControl>();
  final state = Get.find<OderDetailsControl>().state;

  @override
  void initState() {
    super.initState();
    control.getOrderDetails();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colours.system_background_color,
        body:WillPopScope(
          child: GetBuilder<OderDetailsControl>(
            init: control,
            builder: (_) {
              return SizedBox(
                height: MediaQuery.of(context).size.height,
                child: Stack(
                  children: [_orderDetailsUs, _orderDetailsContent],
                ),
              );
            }),
          onWillPop: () async {
            //这里可以响应物理返回键
            return control.onPressed();  
          }));
  }

  Widget get _orderDetailsUs {
    return Container(
      height: 300.w,
      decoration: BoxDecoration(
        image: DecorationImage(
            image: ExactAssetImage(ImageUtils.getImgPath("loan_background")),
            fit: BoxFit.fill),
      ),
    );
  }

  Widget get _orderDetailsContent {
    return SizedBox(
        child: SingleChildScrollView(
      child: Column(
        children: [
          DrawerAppbar(
              backgroundColor: Colors.transparent,
              color: Colours.app_main_color,
              title: 'order_detail'.tr,
              onPressed: control.onPressed),
          _orderTypeContent
        ],
      ),
    ));
  }

  Widget get _orderTypeContent {
    switch (state.orderType) {
      case "application":
        return OrderApplication(
          notice: state.notice,
          model: state.application,
          onPressed: control.onApplication,
        );
      case "apply":
        return OrderApply(
          model: state.apply,
          notice: state.notice,
        );
      case "loan":
        return OrderLoan(
            model: state.loan,
            repayment: control.repayment,
            rollover: control.rollover,
            notice: state.notice);
      default:
    }
    return _empty;
  }

  Widget get _empty {
    return Container(
        padding: EdgeInsets.fromLTRB(27.w, 26.w, 27.w, 36.w),
        margin: EdgeInsets.symmetric(
            horizontal: (MediaQuery.of(context).size.width - 330.w) / 2,
            vertical: 15.w),
        width: 330.w,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(10.w)),
          color: Colours.app_main_color,
        ),
        child: OrderEmpty(empty: state.notice));
  }
}

class OrderLoan extends StatelessWidget {
  final LoanModel? model;
  final Function()? repayment;
  final Function()? rollover;
  final String notice;
  const OrderLoan(
      {super.key,
      required this.model,
      required this.repayment,
      required this.rollover,
      required this.notice});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Image.asset(
          ImageUtils.getImgPath("order_loan_imge"),
          fit: BoxFit.fill,
          width: 154.w,
          height: 84.w,
        ),
        Container(
          margin: EdgeInsets.symmetric(vertical: 15.w),
          child: Text(
            "${"tover".tr}${model!.creditAmount ?? ""}${"amount_activated".tr}",
            style: TextStyle(
              color: Colours.app_main_color,
              fontSize: 12.sp,
            ),
          ),
        ),
        Container(
          padding: EdgeInsets.fromLTRB(30.w, 20.w, 30.w, 8.w),
          margin: EdgeInsets.symmetric(
              horizontal: (MediaQuery.of(context).size.width - 330.w) / 2,
              vertical: 5.w),
          width: 330.w,
          decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(10.w)),
              color: Colours.app_main_color,
              boxShadow: [
                BoxShadow(
                    color:
                        Colours.stringAnAlphaColor("0xff000000", alpha: 0.08),
                    offset: const Offset(0.0, 15.0), //阴影xy轴偏移量
                    blurRadius: 25.0, //阴影模糊程度
                    spreadRadius: 0.5 //阴影扩散程度
                    )
              ]),
          child: Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: _orderNumber,
              ),
              SizedBox(height: 20.w),
              Column(children: _orderDetails)
            ],
          ),
        ),
        Column(children: _repay_list),
        Container(
          margin: EdgeInsets.fromLTRB(33.w, 33.w, 33.w, 0),
          child: Text(
            notice,
            style: TextStyle(
              color: Colours.app_forward_color,
              fontSize: 10.sp,
            ),
             textAlign: TextAlign.left,
          ),
        ),
        _repaymentOr_rollover_repayment
      ],
    );
  }

//repay 为 true 代表用户可以还款（有还款按钮
  Widget get _repayment {
    if (model!.repay == false) {
      return Container();
    }
    return Container(
        margin: EdgeInsets.fromLTRB(0, 65.w, 0, 35.w),
        child: SizedBox(
          width: 310.w,
          child: SPButton(
            minHeight: 42.w,
            fontSize: 18.sp,
            text: "repayment".tr,
            textColor: Colours.app_main_background_color,
            radius: 21.w,
            side: const BorderSide(
              color: Colours.app_main_background_color,
              width: 1.0,
            ),
            onPressed: repayment,
          ),
        ));
  }

 //零时改下
  Widget get _repaymentOr_rollover_repayment {
        // return _repaymentOr_rollover;
    if (model!.repay == true && model!.extend == true) {
      return _repaymentOr_rollover;
    }
    return _repayment;
  }

  //还款 展期 
  Widget get _repaymentOr_rollover {
    return Container(
        margin: EdgeInsets.fromLTRB(0, 45.w, 0, 20.w),
        child: Container(
          margin: EdgeInsets.symmetric(
              horizontal: (MediaQuery.of(context).size.width - 280.w) / 2,
              vertical: 0),
          width: 280.w,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              SizedBox(
                width: 116.w,
                child: SPButton(
                  minHeight: 42.w,
                  fontSize: 18.sp,
                  text:  "repayment".tr,
                  textColor: Colours.app_main_background_color,
                  radius: 21.w,
                  side: const BorderSide(
                    color: Colours.app_main_background_color,
                    width: 1.0,
                  ),
                  onPressed: repayment,
                ),
              ),
              SizedBox(
                width: 116.w,
                child: SPButton(
                  minHeight: 42.w,
                  fontSize: 18.sp,
                  text: "roll".tr,
                  radius: 21.w,
                  textColor: Colours.app_main_color,
                  backgroundColor: Colours.app_main_background_color,
                  onPressed: rollover,
                ),
              ),
            ],
          ),
        ));
  }

  static BuildContext context =
      Global.navigatorKey.currentState!.overlay!.context;

  List<Widget> get _repay_list {
    if (model!.repayList == null || model!.repayList!.isEmpty) {
      return [Container()];
    }
    return model!.repayList!.map((LoanModelRepayList details) {
      return Container(
        padding: EdgeInsets.fromLTRB(10.w, 17.w, 21.w, 14.w),
        margin: EdgeInsets.symmetric(
            horizontal: (MediaQuery.of(context).size.width - 330.w) / 2,
            vertical: 5.w),
        width: 330.w,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(10.w)),
            color: Colours.app_main_color,
            boxShadow: [
              BoxShadow(
                  color: Colours.stringAnAlphaColor("0xff000000", alpha: 0.08),
                  offset: const Offset(0.0, 15.0), //阴影xy轴偏移量
                  blurRadius: 25.0, //阴影模糊程度
                  spreadRadius: 0.5 //阴影扩散程度
                  )
            ]),
        child: Column(
          children: [
            _payable(details: details),
            Container(
              margin: EdgeInsets.fromLTRB(0, 10.w, 0, 8.w),
              child: const DashedLine(),
            ),
            _exceed(details: details),
            _payment(details: details),
          ],
        ),
      );
    }).toList();
  }

  Widget _payment({LoanModelRepayList? details}) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(children: [
          Text(
            "pay_amount".tr,
            style: TextStyle(
              color: Colours.app_text_color,
              fontSize: 14.sp,
            ),
          ),
          SizedBox(width: 32.w),
          //truely_repay_amount
          Text(
            details!.truelyRepayAmount.toString(),
            style: TextStyle(
              color: Colours.app_text_color,
              fontSize: 14.sp,
            ),
          )
        ]),
        Text(
          "${"pay_time".tr}:${details.truelyRepayAt}",
          style: TextStyle(
            color: Colours.app_text_color,
            fontSize: 14.sp,
          ),
        )
      ],
    );
  }

  Widget _payable({LoanModelRepayList? details}) {
    return Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
      Text(
        "${"repayment_amount".tr}:${details!.expectRepayAmount}",
        style: TextStyle(
            color: Colours.app_black_text_color,
            fontSize: 14.sp,
            fontWeight: FontWeight.w500),
      ),
      Text(
        "${"repayment_time".tr}:${details.expectRepayAt}",
        style: TextStyle(
            color: Colours.app_black_text_color,
            fontSize: 12.sp,
            fontWeight: FontWeight.w400),
      )
    ]);
  }

  Widget _exceed({LoanModelRepayList? details}) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(children: [
              Text(
                "late_penalty".tr,
                style: TextStyle(
                  color: Colours.app_text_color,
                  fontSize: 14.sp,
                ),
              ),
              SizedBox(width: 32.w),
              Text(
                details!.expectPenaltyInterest.toString(),
                style: TextStyle(
                  color: Colours.app_text_color,
                  fontSize: 14.sp,
                ),
              )
            ]),
            Row(children: [
              Text(
                "late_day".tr,
                style: TextStyle(
                  color: Colours.app_text_color,
                  fontSize: 14.sp,
                ),
              ),
              SizedBox(width: 32.w),
              Text(
                details.overdueDays.toString(),
                style: TextStyle(
                  color: Colours.app_text_color,
                  fontSize: 14.sp,
                ),
              )
            ]),
          ],
        ),
        Text(
          details.status.toString(),
          style: TextStyle(
              color: Colours.app_main_background_color,
              fontSize: 18.sp,
              fontWeight: FontWeight.w400),
        ),
      ],
    );
  }

  List<Widget> get _orderDetails {
    if (model!.details == null || model!.details!.isEmpty) {
      return [Container()];
    }
    return [
      {
        "title": model!.details![0].key ?? "",
        "value": model!.details![0].value ?? ""
      },
      {
        "title": model!.details![1].key ?? "",
        "value": model!.details![1].value ?? ""
      },
    ].map((value) {
      return Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
        Text(
          value["title"].toString(),
          style: TextStyle(
            color: Colours.app_text_color,
            fontSize: 14.sp,
          ),
        ),
        Text(
          value["value"].toString(),
          style: TextStyle(
            color: Colours.app_line_color,
            fontSize: 14.sp,
          ),
        ),
      ]);
    }).toList();
  }

  List<Widget> get _orderNumber {
    return [
      {"title": "repayment_amount".tr, "value": model!.repayAmount ?? ""},
      {"title": "repayment_time".tr, "value": model!.repayTime ?? ""},
      {"title": "order_status".tr, "value": model!.status ?? ""}
    ].map((value) {
      return Column(
        children: [
          Text(
            value["title"].toString(),
            style: TextStyle(
                color: Colours.app_black_text_color,
                fontSize: 14.sp,
                fontWeight: FontWeight.w500),
          ),
          SizedBox(height: 13.w),
          Text(
            value["value"].toString(),
            style: TextStyle(
                color: Colours.app_main_background_color,
                fontSize: 18.sp,
                fontWeight: FontWeight.w500),
          ),
        ],
      );
    }).toList();
  }
}

class OrderApply extends StatelessWidget {
  final ApplyModel? model;
  final String notice;
  const OrderApply({super.key, required this.model, required this.notice});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          padding: EdgeInsets.fromLTRB(25.w, 34.w, 25.w, 54.w),
          // padding: EdgeInsets.symmetric(vertical: 15.w, horizontal: 25.w),
          margin: EdgeInsets.symmetric(
              horizontal: (MediaQuery.of(context).size.width - 330.w) / 2,
              vertical: 15.w),
          width: 330.w,
          decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(10.w)),
              color: Colours.app_main_color,
              boxShadow: [
                BoxShadow(
                    color:
                        Colours.stringAnAlphaColor("0xff000000", alpha: 0.08),
                    offset: const Offset(0.0, 15.0), //阴影xy轴偏移量
                    blurRadius: 25.0, //阴影模糊程度
                    spreadRadius: 0.5 //阴影扩散程度
                    )
              ]),
          child: Column(
            children: [
              Text(
                "amount_borrowed_unit".tr,
                style: TextStyle(
                  color: Colours.app_black_text_color,
                  fontSize: 11.sp,
                ),
              ),
              Text(
                model!.borrowAmount ?? "",
                style: TextStyle(
                  color: Colours.app_main_text_color,
                  fontSize: 42.sp,
                  fontWeight: FontWeight.w700
                ),
              ),
              Container(
                margin: EdgeInsets.fromLTRB(0, 24.w, 0, 0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      "order_status".tr,
                      style: TextStyle(
                        color: Colours.app_text_color,
                        fontSize: 14.sp,
                      ),
                    ),
                    Text(
                      model!.status ?? "",
                      style: TextStyle(
                          color: Colours.app_order_state_text_color,
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w600),
                    ),
                  ],
                ),
              ),
              Container(
              margin: EdgeInsets.fromLTRB(0, 18.w, 0, 27.w),
              child: const DashedLine(),
            ),
              Column(children: _orderNumber),
            ],
          ),
        ),
        Container(
          margin: EdgeInsets.fromLTRB(35.w, 36.w, 35.w, 0),
          child: Text(
            notice,
            style: TextStyle(
              color: Colours.app_forward_color,
              fontSize: 10.sp,
            ),
             textAlign: TextAlign.left,
          ),
        ),
        Container(
          margin: EdgeInsets.fromLTRB(0, 121.w, 0, 46.w),
          width: 310.w,
          child: SPButton(
            minHeight: 50.w,
            fontSize: 18.sp,
            text: "apply_submitted".tr,
            textColor: Colours.app_main_color,
            backgroundColor: Colours.app_unclickable_color,
            radius: 25.w,
            onPressed: () {},
          ),
        ),
      ],
    );
  }

  List<Widget> get _orderNumber {
    if (model!.details == null || model!.details!.isEmpty) {
      return [
        Container()
      ];
    }
    return model!.details!.map((ApplyModelDetails details) {
      return Container(
          margin: EdgeInsets.symmetric(vertical: 5.w),
          child:
              Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
            Text(
              details.key ?? "",
              style: TextStyle(
                color: Colours.app_text_color,
                fontSize: 14.sp,
              ),
            ),
            Text(
              details.value ?? "",
              style: TextStyle(
                color: Colours.app_line_color,
                fontSize: 14.sp,
              ),
            ),
          ]));
    }).toList();
  }
}

class OrderApplication extends StatelessWidget {
  final ApplicationModel? model;
  final Function()? onPressed;
  final String notice;
  const OrderApplication(
      {super.key,
      required this.model,
      required this.onPressed,
      required this.notice});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text(
          "order_status".tr,
          style: TextStyle(
            color: Colours.app_main_color,
            fontSize: 14.sp,
          ),
        ),
        SizedBox(height: 20.w),
        Text(
          model!.status ?? "",
          style: TextStyle(
            color: Colours.app_main_color,
            fontSize: 30.sp,
            fontWeight:FontWeight.w600
          ),
        ),
        Container(
          padding: EdgeInsets.symmetric(horizontal: 20.w,vertical:21.w ),
          margin: EdgeInsets.symmetric(
              horizontal: (MediaQuery.of(context).size.width - 330.w) / 2,
              vertical: 15.w),
          width: 330.w,
          decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(10.w)),
              color: Colours.app_main_color,
              boxShadow: [
                BoxShadow(
                    color:
                        Colours.stringAnAlphaColor("0xff000000", alpha: 0.09),
                    offset: const Offset(0.0, 15.0), //阴影xy轴偏移量
                    blurRadius: 25.0, //阴影模糊程度
                    spreadRadius: 0.5 //阴影扩散程度
                    )
              ]),
          child: Column(children: _orderNumber),
        ),
        Container(
          margin: EdgeInsets.fromLTRB(33.w, 36.w, 33.w, 0),
          child: Text(
            notice,
            style: TextStyle(
              color: Colours.app_forward_color,
              fontSize: 10.sp,
            ),
             textAlign: TextAlign.left,
          ),
        ),
        Container(
          margin: EdgeInsets.fromLTRB(0, 273.w, 0, 46.w),
          width: 310.w,
          child: SPButton(
            minHeight: 50.w,
            fontSize: 18.sp,
            text: model!.button ?? "",
            textColor: Colours.app_main_color,
            backgroundColor: Colours.app_main_background_color,
            radius: 25.w,
            onPressed: onPressed,
          ),
        ),
      ],
    );
  }

  List<Widget> get _orderNumber {
    if (model!.details == null || model!.details!.isEmpty) {
      return [Container()];
    }
    return model!.details!.map((ApplicationModelDetails details) {
      return Container(
          margin: EdgeInsets.symmetric(vertical: 10.w),
          child:
              Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
            Text(
              details.key ?? "order_number".tr,
              style: TextStyle(
                color: Colours.app_text_color,
                fontSize: 14.sp,
              ),
            ),
            Text(
              details.value ?? "order_time".tr,
              style: TextStyle(
                color: Colours.app_line_color,
                fontSize: 14.sp,
              ),
            ),
          ]));
    }).toList();
  }
}
